package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.Calendar;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * Created by zxh on 16/12/10.
 */

public class ReplyListModel extends XListModel {
    private String replyContent;
    private CommentItem comment;

    public CommentItem getComment() {
        return comment;
    }

    public void setComment(CommentItem comment) {
        this.comment = comment;
    }

    @Bindable
    public String getReplyContent() {
        return replyContent;
    }

    public void setReplyContent(String replyContent) {
        this.replyContent = replyContent;
        notifyPropertyChanged(BR.replyContent);
    }

    public ReplyListModel(Context context, CommentItem commentItem) {
        super(context);
        super.getHandler().setTitle("评论回复");
        this.comment = commentItem;
        load(1);
    }

    @Override
    public void load(final int page) {
        final XListAdapterInterface adapter = getAdapter();
        adapter.clear(true);

        adapter.setVariableId(BR.comment);
        final Activity activity = (Activity) getContext();
        new Thread(new Runnable() {
            @Override
            public void run() {
                final ArrayList result = WebApi.getReplyCommentList(getContext(), comment.getCommentid());
                try {
                    for (Object item : result) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        ReplyItem replyItem = new ReplyItem(activity, R.layout.i_reply);
                        replyItem.setId(Utils.getIntValue(map.get("id")));
                        replyItem.setUid(Utils.getIntValue(map.get("uid")));
                        replyItem.setCommentid(Utils.getIntValue(map.get("commentid")));
                        replyItem.setCommentcontent((String) map.get("replycontent"));
                        replyItem.setCommenttime(Utils.getDateFromFormat(map.get("replytime"), "yyyy-MM-dd HH:mm:ss"));
                        replyItem.setJob((String) map.get("job"));
                        replyItem.setIndustry((String) map.get("industry"));
                        replyItem.setAvatar((String) map.get("avatar"));
                        replyItem.setName((String) map.get("nickname"));
                        adapter.add(replyItem);
                    }
                    if (result.size() != 0) {
                        setCurrentPage(page);
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                notifyDataSetChanged(adapter);
            }
        }).start();
    }

    /**
     * 发表回复
     *
     * @param view
     */
    public void onSendReply(View view) {
        final String content = getReplyContent();
        if (Utils.isNullOrEmpty(content)) {
            showToast("评论内容不能为空");
            return;
        }
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!WebApi.allowAction(getContext())) {
                    LoadingDialog.getInstance(getContext()).dismiss();
                    return;
                }
                if (WebApi.replyComment(getContext(), comment.getCommentid(), content)) {
                    ReplyItem item = new ReplyItem(getContext(), R.layout.i_reply);
                    item.setCommentid(comment.getCommentid());
                    item.setCommenttime(Calendar.getInstance().getTime());
                    item.setUid(User.getCurrentUser().getUid());
                    item.setCommentcontent(content);
                    setReplyContent("");
                    LoadingDialog.getInstance(getContext()).dismiss();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            comment.setReply(comment.getReply() + 1);
                            load(1);
                        }
                    });
                }
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }
}
